package com.pine.sunshineelder.elderfood.service.impl;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageInfo;
import com.pine.sunshineelder.common.StringHelper;
import com.pine.sunshineelder.common.dao.ElderFoodDao;
import com.pine.sunshineelder.common.dto.QueryResult;
import com.pine.sunshineelder.common.entity.ElderFoodEntity;
import com.pine.sunshineelder.common.exception.ThisSystemException;
import com.pine.sunshineelder.common.validator.ValidateHelper;
import com.pine.sunshineelder.elderfood.ao.ElderFoodAddAo;
import com.pine.sunshineelder.elderfood.ao.ElderFoodModAo;
import com.pine.sunshineelder.elderfood.service.ElderFoodService;
/**
 * 
 * @author 黄天静
 *
 */
@Service
public class ElderFoodServicelmpl implements ElderFoodService, StringHelper {
	@Autowired
	private ElderFoodDao efdao;

	@Override
	public ElderFoodEntity addElderFood(ElderFoodAddAo ao) throws Exception {
		// 1.验证参数
		// 1.1空字符串的验证
		String aideId = ValidateHelper.$("护工id", ao.getAideId());
		String elderId = ValidateHelper.$("老人id", ao.getElderId());
		LocalDate mealDate = ao.getMealDate();
		String mealTime = ValidateHelper.$("用餐时间", ao.getMealTime());
		String foods = ValidateHelper.$("营养套餐", ao.getFoods());
		String foodImage = ValidateHelper.$("食物图片", ao.getFoodImage());
		LocalDateTime createTime = LocalDateTime.now();
		// 3.组装结果
		ElderFoodEntity ef = new ElderFoodEntity();
		String id = uuid();
		ef.setId(id);
		ef.setAideId(aideId);
		ef.setElderId(elderId);
		ef.setMealDate(mealDate);
		ef.setMealTime(mealTime);
		ef.setFoods(foods);
		ef.setFoodImage(foodImage);
		ef.setCreateTime(createTime);
		efdao.insert(ef);
		return ef;
	}

	@Override
	public void deleteElderFood(String id) throws Exception {
		// 1 验证清理参数
		id = ValidateHelper.$("id", id);
		// 2执行业务逻辑
		efdao.delete(id);
	}

	@Override
	public ElderFoodEntity modifyUser(String id, ElderFoodModAo ao) throws Exception {
		ElderFoodEntity ef = new ElderFoodEntity();
		// 1.验证参数
		// 1.1空字符串的验证
		id = ValidateHelper.$("用膳数据id", ao.getId());
		// 膳食日期
		LocalDate mealDate = ao.getMealDate();
		// 膳食的餐点
		String mealTime = ValidateHelper.$("用餐餐点", ao.getMealTime());
		// 膳食内容
		String foods = ValidateHelper.$("膳食", ao.getFoods());
		// 膳食图片信息
		String foodImage = ValidateHelper.$("膳食信息", ao.getFoodImage());
		// 3.组装结果
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("mealDate", mealDate);
		map.put("mealTime", mealTime);
		map.put("foods", foods);
		map.put("foodImage", foodImage);
		efdao.update(id, map);
		return ef;
	}

	@Override
	public QueryResult<ElderFoodEntity> queryByKey(String key, Integer pageNo, Integer pageSize) throws Exception {
		key = trimAsNull(key);
		if (key != null) {
			// 字符串的拼接，代表前后的数据不限
			key = "%" + key + "%";
		}
		if (pageNo == null) {
			pageNo = 1;
		}
		if (pageSize == null) {
			pageSize = 10;
		}
		List<ElderFoodEntity> rows = efdao.selectByKey(key, pageNo, pageSize);

		PageInfo pageInfo = new PageInfo(rows);

		int totalRows = (int) pageInfo.getTotal();

		QueryResult result = new QueryResult();
		result.setPageNo(pageNo);
		result.setPageSize(pageSize);
		result.setTotalRows(totalRows);
		result.setRows(rows);
		return result;
	}

	@Override
	public ElderFoodEntity queryById(String id) throws Exception {

		// 1.验证和清理参数
		id = ValidateHelper.$("用膳数据id", id);
		// 2执行业务逻辑
		ElderFoodEntity ef = efdao.selectById(id);
		if (ef == null) {
			throw new ThisSystemException("id.not-exisits", "非法id");
		}
        
		return ef;
	}

}
